namespace comp {
    const DeviceMotion = "devicemotion"
    export class HorizonController implements EventListenerObject, GameController {
        private _initAngle: number
        private _initSpeed: number
        private _angle: number
        constructor() {
            this._initAngle = 0
            this._angle = 0
        }
        public Type(): GameControllerKind { return GameControllerKind.HorizonController }

        public get Angle(): number { return this._angle }

        public start(angle: number, speed: number) {
            this._initAngle = angle
            this._initSpeed = speed != 0 ? speed : 1
            this.reset()
            window.addEventListener(DeviceMotion, this, true)
        }
        public end() {
            window.removeEventListener(DeviceMotion, this, true)
        }
        public reset() {
            this._angle = this._initAngle
            game.notify(ntf.JoyStickChange, { Target: this, Distance: 0, Angle: this._angle }, this)
        }
        public handleEvent(evt: Event): void {
            let e = <DeviceMotionEvent>(evt)
            this._angle += e.rotationRate.gamma * e.interval / 1000 * 1.5
            game.notify(ntf.JoyStickChange, { Target: this, Distance: 0, Angle: this._angle }, this)
        }
    }
}